package com.mvvm.start.base.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import java.util.*


/**
 * @功能:Service工具类
 * @User Lmy
 * @Creat 2020/12/17 18:47
 * @Compony 永远相信美好的事情即将发生
 */
object ServiceUtils {

    /**
     * 判断服务是否开启
     *
     * @return
     */
    fun isServiceRunning(context: Context, ServiceName: String): Boolean {
        if (TextUtils.isEmpty(ServiceName)) {
            return false
        }
        val myManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningService =
            myManager.getRunningServices(1000) as ArrayList<ActivityManager.RunningServiceInfo>
        for (i in runningService.indices) {
            runningService[i].service.className.loge("服务已经在运行了～不要重复启动哦")
            if (runningService[i].service.className == ServiceName) {
                return true
            }
        }
        return false
    }

    //判断权限
    fun commonROMPermissionCheck(context: Context?): Boolean {
        var result = true
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                val clazz: Class<*> = Settings::class.java
                val canDrawOverlays =
                    clazz.getDeclaredMethod("canDrawOverlays", Context::class.java)
                result = canDrawOverlays.invoke(null, context) as Boolean
            } catch (e: Exception) {
                Log.e("ServiceUtils", Log.getStackTraceString(e))
            }
        }
        return result
    }
}